package com.feinno.urcs.group.pd.servlet;

import com.feinno.msrp.MsrpSession;
import com.feinno.urcs.group.api.GroupService;
import com.feinno.urcs.group.pd.entity.GroupFileArgs;
import com.feinno.urcs.nai.IMPU;
import com.feinno.urcs.sipservlet.LargeMessageServlet;
import com.feinno.urcs.sipservlet.SipServletRequest;
import com.feinno.urcs.sipservlet.msrp.MsrpSessionFactory;
import com.feinno.urcs.types.MobileNo;
import com.feinno.urcs.utils.SipHeaderUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

/**
 * Created by wuhao on 5/11/15.
 */
public class PdGroupFileTransferServlet extends LargeMessageServlet {

    private static final Logger LOGGER = LoggerFactory.getLogger(PdGroupFileTransferServlet.class);
    private GroupService groupService;
    private MsrpSessionFactory msrpFactory;
    private static final String FILE_KEY = "FILE_KEY";

    @Override
    protected OutputStream getOutputStream(SipServletRequest inviteRequest) {
        LOGGER.info("PdGroupFileTransferServlet createOutputStream Begin");

        //获取发送者接受者
        IMPU sender = SipHeaderUtils.getFromUser(inviteRequest);
        IMPU grouper = SipHeaderUtils.getToUser(inviteRequest);
        ByteArrayOutputStream outputStream = null;

        try {
            //检查群是否存在及其他状态
            groupService.check( MobileNo.parseFrom(sender.getUser()), grouper.toUri().toString());
            //
            outputStream = new ByteArrayOutputStream();
            inviteRequest.getSession().put(FILE_KEY, outputStream);
        } catch (Exception e) {
            LOGGER.error("PdGroupFileTransferServlet check error {}", e);
            throw new RuntimeException(e);
        }
        LOGGER.info("PdGroupFileTransferServlet createOutputStream End");
        return outputStream;
    }

    @Override
    protected MsrpSession createMsrpSession(SipServletRequest sipServletRequest) {
        return msrpFactory.createSession();
    }

    @Override
    protected void event(Event event) {
        if (event.getState() == State.SUCCESS) {
            this.receiveDone(event.getInviteReuqest());
        } else if (event.getState() == State.FAILED) {
            LOGGER.error("", event.getError());
        }
    }

    protected void receiveDone(SipServletRequest sipServletRequest) {
        LOGGER.info("PdGroupFileTransferServlet receiveDone Begin");
        LOGGER.info("Received sipServletRequest:{}", sipServletRequest.toString());


        //构建
        GroupFileArgs gfa = new GroupFileArgs();

        IMPU sender = SipHeaderUtils.getFromUser(sipServletRequest);
        IMPU grouper = SipHeaderUtils.getToUser(sipServletRequest);
        ByteArrayOutputStream outputStream = (ByteArrayOutputStream)sipServletRequest.getSession().get(FILE_KEY);
        gfa.setSender(sender);
        gfa.setReceiver(grouper);
//        gfa.setFileId(fileId);

        //发送群文件
        //groupService.sendGroupFile(gfa);

        LOGGER.info("PdGroupFileTransferServlet receiveDone End");
    }
}
